call testRealSpeechMethods

procedure testRealSpeechMethods
	clearinfo
	.tableid = Create Table with column names... results 0 Filename StartTime EndTime Vowel Frequency F1b F2b F3b B1b B2b B3b F1r F2r F3r B1r B2r B3r


	Create Strings as directory list... dirList /home/xtroce/Downloads/shs/labels
	.nrdirs = Get number of strings
	for .idir to .nrdirs
		select Strings dirList
		.dirname$ = Get string... .idir
		.curdir$ = "/home/xtroce/Downloads/shs/labels/'.dirname$'/" 
		Create Strings as file list... fileList '.curdir$'
		.nrfiles = Get number of strings
		for .ifile to .nrfiles
			select Strings fileList
			.filename$ = Get string... .ifile
			.filename$ = .filename$ - ".phoneme"
			printline '.dirname$' '.filename$'
			call analyzeLabel '.filename$' /home/xtroce/Downloads/shs/ '.dirname$' .tableid
		endfor
	endfor
endproc

procedure analyzeSingleSound .freq .start .end .sndid .tableid .row
	select .sndid
	call analyzeFormants .sndid .freq 0 .start .end
		select .tableid
		Set numeric value... .row F1b analyzeFormants.res[1]
                Set numeric value... .row F2b analyzeFormants.res[2]
                Set numeric value... .row F3b analyzeFormants.res[3]
                Set numeric value... .row B1b analyzeFormants.res[4]
                Set numeric value... .row B2b analyzeFormants.res[5]
                Set numeric value... .row B3b analyzeFormants.res[6]

	select .sndid
	call analyzeFormants .sndid .freq 1.5 .start .end
		select .tableid
		Set numeric value... .row F1r analyzeFormants.res[1]
                Set numeric value... .row F2r analyzeFormants.res[2]
                Set numeric value... .row F3r analyzeFormants.res[3]
                Set numeric value... .row B1r analyzeFormants.res[4]
                Set numeric value... .row B2r analyzeFormants.res[5]
                Set numeric value... .row B3r analyzeFormants.res[6]
endproc

procedure analyzeFormants .sndid .max_freq .std_dev .start .end
        select .sndid
	if .std_dev > 0
        	#call robust formant analyzer
        	.formant = To Formant (robust)... 0.0 5 .max_freq 0.025 50 .std_dev 5 0.000001
        	.res[1] = Get mean... 1 .start .end Hertz
        	.res[2] = Get mean... 2 .start .end Hertz
        	.res[3] = Get mean... 3 .start .end Hertz
        	.res[4] = Get quantile of bandwidth... 1 .start .end Hertz 0.5
        	.res[5] = Get quantile of bandwidth... 2 .start .end Hertz 0.5
        	.res[6] = Get quantile of bandwidth... 3 .start .end Hertz 0.5
		Remove
	else
        	#call burg formant analyzer
        	.formant = To Formant (burg)... 0.0 5 .max_freq 0.025 50
        	.res[1] = Get mean... 1 .start .end Hertz
        	.res[2] = Get mean... 2 .start .end Hertz
        	.res[3] = Get mean... 3 .start .end Hertz
        	.res[4] = Get quantile of bandwidth... 1 .start .end Hertz 0.5
        	.res[5] = Get quantile of bandwidth... 2 .start .end Hertz 0.5
        	.res[6] = Get quantile of bandwidth... 3 .start .end Hertz 0.5
        	Remove
	endif
endproc


procedure analyzeLabel .name$ .path$ .pathext$ .tableid

	.sex$ = left$ ( .name$, 1)
	if .sex$ = "M"
		.maxfreq = 4500
#	else
#		.maxfreq = 5500
#	endif

	call openSound '.name$' '.path$' '.pathext$'
	call getLabelTimes openSound.label

	printline total amount of iterations... 'getLabelTimes.num'

	for i from 1 to getLabelTimes.num
		printline analyzing sound part... 'i'

		select .tableid
		.v$ = getLabelTimes.allVowels$[i]
		call initRow .tableid getLabelTimes.start[i] getLabelTimes.end[i] 'openSound.so1_name$' '.v$'
		call initRow .tableid getLabelTimes.start[i] getLabelTimes.end[i] 'openSound.so2_name$' '.v$'
		.row =  Get number of rows
		.starttime = getLabelTimes.start[i]
		.endtime = getLabelTimes.end[i]
		.vowel$ = getLabelTimes.allVowels$[i]

		
		call analyzeSingleSound .maxfreq getLabelTimes.start[i] getLabelTimes.end[i] openSound.s1 .tableid .row-1
		call analyzeSingleSound .maxfreq getLabelTimes.start[i] getLabelTimes.end[i] openSound.s2 .tableid .row
	endfor

	select openSound.s1
	plus openSound.s2
	plus openSound.label
	Remove

	endif
endproc

procedure initRow .tableid .starttime .endtime .name$ .vowel$
		Append row
		.row = Get number of rows
		Set numeric value... .row StartTime .starttime
	        Set numeric value... .row EndTime .endtime
		Set string value... .row Filename '.name$'
		Set string value... .row Vowel '.vowel$'
endproc	

procedure openSound .name$ .path$ .pathext$
	.so1_name$ = "fm/" + "'.pathext$'" + "/" + "'.name$'"+"_fm.aifc"
	.so2_name$ = "hm/" + "'.pathext$'" + "/" + "'.name$'"+"_hm.aifc"
	.phoneme_name$ = "labels/" + "'.pathext$'" + "/" + "'.name$'" + ".phoneme"
	.s1 = Read from file... '.path$''.so1_name$'
	.s2 = Read from file... '.path$''.so2_name$'
	.label = Read from file... '.path$''.phoneme_name$'
endproc

procedure getLabelTimes .labelfile
	select .labelfile
	.amount = Get number of intervals... 1
	printline total amount of parts found in soundfile... '.amount'
	.num = 0
	
	.vowel$[1] = "i"
	.vowel$[2] = "a"
	.vowel$[3] = "e"
	.vowel$[4] = "o"
	.vowel$[5] = "u"

	for .interval from 1 to .amount
		l$ = Get label of interval... 1 .interval

		.label$ = left$ ( l$, 1)
		for i from 1 to 5
			.v$ = .vowel$[i]
			if .label$ = .v$
				.num = .num + 1
				.allVowels$[.num] = .v$
				.start[.num] = Get start point... 1 .interval
				.end[.num] = Get end point... 1 .interval
			endif
		endfor
	endfor
	printline amount of vowels found... '.num'

endproc
